package com.jz.netthings.application.upload.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.application.providerCreate.service.ProviderInfoService;
import com.jz.netthings.application.upload.service.UploadService;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.support.SimpleCoreResponse;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("adscm/filesystem")
public class FileServerController {
    @Autowired
    UploadService uploadService;
    @Autowired
    ProviderInfoService providerInfoService;

    @RequestMapping("/abc")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file){
        String url = this.uploadService.uploadImage(file);
        if (StringUtils.isBlank(url)) {
            // url为空，证明上传失败
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        // 返回200，并且携带url路径
        return ResponseEntity.ok(url);
    }

    @RequestMapping("/downLoadImage")
    public String  downLoadImage(){

        Wrapper<ProviderInfo> wrapper = new EntityWrapper<>();
        wrapper.gt("createDate","2018-01-01");
        List<ProviderInfo> infoList = providerInfoService.selectList(wrapper);

        String path = "E:\\tempImages\\";
        for (ProviderInfo info : infoList) {
            String photo = info.getPhoto();
            String idCardPhoto = info.getIdCardPhoto();
            String idCardPhoto1 = info.getIdCardPhoto1();

            String idNo = info.getIdNo();
            uploadService.downloadImageReturnPath(photo, path+idNo+".jpg");
            uploadService.downloadImageReturnPath(idCardPhoto,path+idNo+"c.jpg");
            uploadService.downloadImageReturnPath(idCardPhoto1,path+idNo+"c1.jpg");

        }

        return "sucess";
    }
    @RequestMapping("/upLoadImage")
    public String  upLoadImage() throws AdscmSystemException {

        Wrapper<ProviderInfo> wrapper = new EntityWrapper<>();
        wrapper.gt("createDate","2018-01-01");
        List<ProviderInfo> infoList = providerInfoService.selectList(wrapper);

        String path = "E:\\tempImages\\";
        for (ProviderInfo info : infoList) {
            String photo = info.getPhoto();
            String idCardPhoto = info.getIdCardPhoto();
            String idCardPhoto1 = info.getIdCardPhoto1();

            String idNo = info.getIdNo();
            photo = uploadService.uploadImageByBytes(AdscmBaseUtils.getInstance().getFileBytes(path+idNo+".jpg"));
            idCardPhoto = uploadService.uploadImageByBytes(AdscmBaseUtils.getInstance().getFileBytes(path+idNo+"c.jpg"));
            idCardPhoto1 = uploadService.uploadImageByBytes(AdscmBaseUtils.getInstance().getFileBytes(path+idNo+"c1.jpg"));

            info.setPhoto(photo);
            info.setIdCardPhoto(idCardPhoto);
            info.setIdCardPhoto1(idCardPhoto1);
            System.out.println("上传["+info.getName()+"]的照片成功....");
            providerInfoService.updateById(info);
        }

        return "sucess";
    }





}